using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SBPweb.Themes.Windows.Skins;
using SBPweb.Themes.Windows;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;

namespace SBPweb.Controls.Windows.Common
{
	/// <summary>
	/// Class of ScrollControl
	/// </summary>
	[ToolboxBitmap(typeof(System.Windows.Forms.ScrollBar))]
	public partial class ScrollControl : SBPweb.Controls.Windows.Common.Panel, ISkinnable
	{
		#region Private members

		const int ORIENTATION_OFFSET = 8;
		const int  IMAGE_OFFSET = 4;
		const uint WM_PAINT = 0x000F;
		const uint WM_SIZE = 0x0005;

		private int contentWidth = 0;
		private int contentHeight = 0;

		private Margin mButtonSizingMargin;
		private Margin mShaftSizingMarginHoriz;
		private Margin mShaftSizingMarginVert;
		private Margin mThumbSizingMarginHoriz;
		private Margin mThumbSizingMarginVert;
		
		private MouseEventState mMState = MouseEventState.None;
		private MouseEventState mMStateButtonFirstHoriz = MouseEventState.None;
		private MouseEventState mMStateButtonSecondHoriz = MouseEventState.None;
		private MouseEventState mMStateButtonFirstVert = MouseEventState.None;
		private MouseEventState mMStateButtonSecondVert = MouseEventState.None;
		private MouseEventState mMStateShaftFirstHoriz = MouseEventState.None;
		private MouseEventState mMStateShaftSecondHoriz = MouseEventState.None;
		private MouseEventState mMStateShaftFirstVert = MouseEventState.None;
		private MouseEventState mMStateShaftSecondVert = MouseEventState.None;
		private MouseEventState mMStateThumbHoriz = MouseEventState.None;
		private MouseEventState mMStateThumbVert = MouseEventState.None;

		private Point buttonFirstHorizPos;
		private Point buttonSecondHorizPos;
		private Point buttonFirstVertPos;
		private Point buttonSecondVertPos;
		private int buttonWidth;
		private int buttonHeight;
		private Point shaftFirstHorizPos;
		private Point shaftSecondHorizPos;
		private int shaftFirstHoriz = 0;
		private int shaftSecondHoriz = 0;
		private Point shaftFirstVertPos;
		private Point shaftSecondVertPos;
		private int shaftFirstVert = 0;
		private int shaftSecondVert = 0;
		private Point thumbHorizPos;
		private Point thumbVertPos;
		private int thumbHorizWidth = 17;
		private int thumbHorizHeight = 17;
		private int thumbVertWidth = 17;
		private int thumbVertHeight = 17;
		
		private bool mScrollH = false;
		private bool mScrollV = false;

		private double valueV;
		private double valueH;

		private int tempWidth;
		private int tempHeight;

		protected int moLargeChange = 10;
		protected int moSmallChange = 1;
		protected int moMinimum = 0;
		protected int moMaximum = 100;
		protected int moValue = 0;

		private bool mHorizThumbDown = false;
		private bool mVertThumbDown = false;
		private Point mThumbDownPoint = new Point();

		private int mMovedThumbHorizOffset;
		private int mMovedThumbVertOffset;
		private int mThumbDownHorizPos;
		private int mThumbDownVertPos;

		#endregion

		#region Protected properties
		protected bool ScrollH
		{
			get
			{
				return mScrollH;
			}
			set
			{
				mScrollH = value;
				
			}
		}

		protected bool ScrollV
		{
			get
			{
				return mScrollV;
			}
			set
			{
				mScrollV = value;
			}
		}

		protected double ValueV
		{
			get
			{
				return valueV;
			}
			set
			{
				valueV = value;
			}
		}

		protected double ValueH
		{ 
			get
			{
				return valueH;
			}
			set
			{
				valueH = value;
			}
		}
		#endregion

		#region Constructor logic
		public ScrollControl()
		{
			SetStyle();
			
			InitializeComponent();
			InitializeContentSize();
			InitializeControlMargins();
			RegisterEventHandlers();

			this.UseDefaultSkin = true;
		}


		public ScrollControl(Skin skin):base()
		{
			mSkin = skin;
		}

		private new void SetStyle()
		{
			base.SetStyle(ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer |ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor, true);
		}
				
		
		[DllImport("USER32.DLL", EntryPoint = "PostMessage",
		CallingConvention = CallingConvention.StdCall)]
		public static extern bool PostMessage(IntPtr HWND, uint msg, IntPtr wparam, IntPtr lparam);

		/*protected override void WndProc(ref Message m)
		{
			base.WndProc(ref m);

			if (m.Msg == WM_PAINT)
			{
				if (drawchild)
				{
					base.WndProc(ref m);
					drawchild = false;

					PostMessage(this.Handle, WM_PAINT, m.WParam, m.LParam);
				}
				else
				{
					OnPaint(new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
					drawchild = true;
				}

				m.Result = new IntPtr(1);
			}
			else
			{
				base.WndProc(ref m);
			}
		}*/

		/*protected override void WndProc(ref Message m)
		{
			base.WndProc(ref m);

			if (m.Msg == WM_PAINT)
			{
				OnPaint(new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
			}
		}*/

		private void InitializeContentSize()
		{
			if (this.innerPanel != null)
			{
				foreach (Control c in this.innerPanel.Controls)
				{
					if (c.Size.Width > contentWidth)
					{
						contentWidth = c.Size.Width;
					}
					if (c.Size.Height > contentHeight)
					{
						contentHeight = c.Size.Height;
					}
				}
			}
		}

		private void InitializeControlMargins()
		{
			if (Skin != null)
			{
				mButtonSizingMargin = Skin.CachedProperties.ScrollButtonSizingMargin;
				mShaftSizingMarginHoriz = Skin.CachedProperties.ScrollShaftHorizSizingMargin;
				mShaftSizingMarginVert = Skin.CachedProperties.ScrollShaftVertSizingMargin;
				mThumbSizingMarginHoriz = Skin.CachedProperties.ScrollThumbHorizSizingMargin;
				mThumbSizingMarginVert = Skin.CachedProperties.ScrollThumbVertSizingMargin;
			}
		}

		private void InitializeInnerPanelSize()
		{ 
			int w = this.ClientRectangle.Width;
			int h = this.ClientRectangle.Height;

			bool verticalLayout = false;

			if (Skin != null)
			{
				Image img = Skin.GetBitmapResource(Skin.KeyPrefix + "common.scrollbutton.png");
				int imgCount = Convert.ToInt32(Skin.SelectSingleNode("Elements/ScrollButton/Image").Attributes["ImageCount"].Value);
				if (Skin.SelectSingleNode("Elements/ScrollButton/Image").Attributes["Layout"] != null)
				{
					if (Skin.SelectSingleNode("Elements/ScrollButton/Image").Attributes["Layout"].Value.ToLower() == "vertical")
					{
						verticalLayout = true;
					}
				}
				this.innerPanel.Dock = DockStyle.None;

				if (ScrollH || this.innerPanel.HorizontalScroll.Visible)
				{
					tempWidth = (verticalLayout)? img.Width : img.Width/imgCount;
					ScrollH = true;
					ValueH = this.HorizontalScroll.Value;
				}
				if (ScrollV || this.innerPanel.VerticalScroll.Visible)
				{
					tempHeight = (verticalLayout)? img.Height/imgCount : img.Height;
					ScrollV = true;
					ValueV = this.HorizontalScroll.Value;
				}

				this.innerPanel.AutoScroll = false;

				this.innerPanel.Width = w - tempWidth;
				this.innerPanel.Height = h - tempHeight;
			}
		}

		protected new void RegisterEventHandlers()
		{
			this.MouseEnter += new EventHandler(this_MouseEnter);
			this.MouseLeave += new EventHandler(this_MouseLeave);
			this.MouseDown += new MouseEventHandler(this_MouseDown);
			this.MouseUp += new MouseEventHandler(this_MouseUp);
			this.MouseHover += new EventHandler(this_MouseHover);
			this.MouseMove += new MouseEventHandler(this_MouseMove);
			DefaultLookAndFeel.OnSkinChanged += new EventHandler(DefaultLookAndFeel_OnSkinChanged);
			this.innerPanel.SizeChanged += new EventHandler(innerPanel_SizeChanged);
		}

		void innerPanel_SizeChanged(object sender, EventArgs e)
		{
			Console.Write("size changed");
		}


		protected override void OnControlAdded(ControlEventArgs e)
		{
			this.Controls.Add(e.Control);

			IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
			DesignerTransaction dt = null;
			IComponentChangeService cService = (IComponentChangeService)GetService(typeof(IComponentChangeService));

			if (DesignMode & host != null)
			{
				dt = host.CreateTransaction("Draw scroll control");
			}

			
			Control c = e.Control;


			if (host != null)
			{
				c = (Control)host.CreateComponent(e.Control.GetType());
			}


			if (c != this.innerPanel)
			{
				this.innerPanel.Controls.Add(c);
				c.PointToClient(c.Location);
				foreach (Control cc in this.innerPanel.Controls)
				{
					Point p = cc.Location;
				}

				if (c.Size.Width > contentWidth)
				{
					contentWidth = c.Size.Width;
				}
				if (c.Size.Height > contentHeight)
				{
					contentHeight = c.Size.Height;
				}
			}

			if (DesignMode && cService != null)
				cService.OnComponentChanging(this, null);

			if (DesignMode & dt != null)
			{
				dt.Commit();
			}

			Invalidate();
			
		}

		void c_LocationChanged(object sender, EventArgs e)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		/*protected override void OnControlRemoved(ControlEventArgs e)
		{
			base.OnControlRemoved(e);
			IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
			DesignerTransaction dt = null;
			IComponentChangeService cService = (IComponentChangeService)GetService(typeof(IComponentChangeService));

			if (DesignMode & host != null)
			{
				dt = host.CreateTransaction("Draw scroll control");
			}

			Control c = e.Control;

			if (host != null)
			{
				host.DestroyComponent(e.Control);
				//if (this.innerPanel != null)
				//{
				//    if (this.innerPanel.Host != null)
				//        c = (Control)this.innerPanel.Host.CreateComponent(e.Control.GetType());
				//}
			}
			if (c != this.innerPanel)
			{
				this.innerPanel.Controls.Add(c);
				if (c.Size.Width > contentWidth)
				{
					contentWidth = c.Size.Width;
				}
				if (c.Size.Height > contentHeight)
				{
					contentHeight = c.Size.Height;
				}
			}
			int l = c.Left;
			int t = c.Top;
			this.innerPanel.AutoScrollPosition = new Point(this.innerPanel.AutoScrollPosition.X + c.Location.X, this.innerPanel.AutoScrollPosition.Y + c.Location.Y);

			if (DesignMode && cService != null)
				cService.OnComponentChanging(this, null);

			if (DesignMode & dt != null)
			{
				dt.Commit();
			}

			Invalidate();
		}*/
		

		#endregion

		#region Custom draw

		protected override void OnPaint(PaintEventArgs e)
		{
			if (Skin == null)
			{
				base.OnPaint(e);
				return;
			}
			
			base.OnPaintBackground(e);

			if (ScrollH)
			{
				DrawButton(e, true, Orientation.Horizontal);
				DrawThumbButton(e, Orientation.Horizontal);
				DrawButton(e, false, Orientation.Horizontal);
				DrawShaft(e, true, Orientation.Horizontal);
				DrawShaft(e, false, Orientation.Horizontal);
			}

			if (ScrollV)
			{
				DrawButton(e, true, Orientation.Vertical);
				DrawThumbButton(e, Orientation.Vertical);
				DrawButton(e, false, Orientation.Vertical);
				DrawShaft(e, true, Orientation.Vertical);
				DrawShaft(e, false, Orientation.Vertical);
			}
		}

		private void DrawShaft(PaintEventArgs e, bool first, Orientation orientation)
		{
			bool verticalLayout;
			System.Drawing.Image img;

			if (orientation == Orientation.Horizontal)
			{
				img = Skin.GetImageFromBitmapResource(Skin.KeyPrefix + "common.scrollshaft.png");

				verticalLayout = false;
				if (Skin.SelectSingleNode("Elements/ScrollShaft/Image").Attributes["Layout"] != null)
				{
					if (Skin.SelectSingleNode("Elements/ScrollShaft/Image").Attributes["Layout"].Value.ToLower() == "vertical")
					{
						verticalLayout = true;
					}
				}
			}
			else
			{
				img = Skin.GetImageFromBitmapResource(Skin.KeyPrefix + "common.scrollshaftvert.png");

				verticalLayout = false;
				if (Skin.SelectSingleNode("Elements/ScrollShaftVert/Image").Attributes["Layout"] != null)
				{
					if (Skin.SelectSingleNode("Elements/ScrollShaftVert/Image").Attributes["Layout"].Value.ToLower() == "vertical")
					{
						verticalLayout = true;
					}
				}
			}

			//int imgCount = 4;
			int imgCount = 0;
			if (Skin.SelectSingleNode("Elements/ScrollShaftVert/Image").Attributes["Layout"] != null)
			{
				imgCount = int.Parse(Skin.SelectSingleNode("Elements/ScrollShaftVert/Image").Attributes["ImageCount"].Value);
			}
			int imgWidth;
			int imgHeight;
			int imgLeft = 0;
			int imgTop = 0;

			if (!verticalLayout)
			{
				imgWidth = (Int32)(img.Width / imgCount);
				imgHeight = img.Height - 1;

				if (!this.Enabled)
				{
					imgLeft = 3 * imgWidth;
				}
				else
				{
					if (first)
					{
						mMState = (orientation == Orientation.Horizontal) ? mMStateShaftFirstHoriz : mMStateShaftFirstVert;

						switch (mMState)
						{
							default:
							case MouseEventState.None:
								imgLeft = 0;
								break;
							case MouseEventState.OnHover:
								imgLeft = 1 * imgWidth;
								break;
							case MouseEventState.OnDown:
								imgLeft = 2 * imgWidth;
								break;
						}
					}
					else
					{
						mMState = (orientation == Orientation.Horizontal) ? mMStateShaftSecondHoriz : mMStateShaftSecondVert;

						switch (mMState)
						{
							default:
							case MouseEventState.None:
								imgLeft = 0;
								break;
							case MouseEventState.OnHover:
								imgLeft = 1 * imgWidth;
								break;
							case MouseEventState.OnDown:
								imgLeft = 2 * imgWidth;
								break;
						}
					}
				}
			}
			else
			{
				imgWidth = img.Width;
				imgHeight = (Int32)(img.Height / imgCount);

				if (!this.Enabled)
				{
					imgTop = 3 * imgHeight;
				}
				else
				{
					if (first)
					{
						mMState = (orientation == Orientation.Horizontal) ? mMStateShaftFirstHoriz : mMStateShaftFirstVert;
						switch (mMState)
						{
							default:
							case MouseEventState.None:
								imgTop = 0;
								break;
							case MouseEventState.OnHover:
								imgTop = 1 * imgHeight;
								break;
							case MouseEventState.OnDown:
								imgTop = 2 * imgHeight;
								break;
						
						}
					}
					else
					{
						mMState = (orientation == Orientation.Horizontal) ? mMStateShaftSecondHoriz : mMStateShaftSecondVert;
						switch (mMState)
						{
							default:
							case MouseEventState.None:
								imgTop = 0;
								break;
							case MouseEventState.OnHover:
								imgTop = 1 * imgHeight;
								break;
							case MouseEventState.OnDown:
								imgTop = 2 * imgHeight;
								break;
						}
					}
				}
			}

			Margin mShaftSizingMargin = (orientation == Orientation.Horizontal)? mShaftSizingMarginHoriz : mShaftSizingMarginVert;

			#region Image parts
			Rectangle iLT = new Rectangle(imgLeft, imgTop, mShaftSizingMargin.Left, mShaftSizingMargin.Top);
			Rectangle iT = new Rectangle(imgLeft + mShaftSizingMargin.Left, imgTop, imgWidth - mShaftSizingMargin.Horizontal, mShaftSizingMargin.Top);
			Rectangle iRT = new Rectangle(imgLeft + imgWidth - mShaftSizingMargin.Right, imgTop, mShaftSizingMargin.Right, mShaftSizingMargin.Top);
			Rectangle iLM = new Rectangle(imgLeft, imgTop + mShaftSizingMargin.Top, mShaftSizingMargin.Left, imgHeight - mShaftSizingMargin.Vertical);
			Rectangle iM = new Rectangle(imgLeft + mShaftSizingMargin.Left, imgTop + mShaftSizingMargin.Top, imgWidth - mShaftSizingMargin.Horizontal, imgHeight - mShaftSizingMargin.Vertical);
			Rectangle iRM = new Rectangle(imgLeft + imgWidth - mShaftSizingMargin.Right, imgTop + mShaftSizingMargin.Top, mShaftSizingMargin.Right, imgHeight - mShaftSizingMargin.Vertical);
			Rectangle iLB = new Rectangle(imgLeft, imgTop + imgHeight - mShaftSizingMargin.Bottom, mShaftSizingMargin.Left, mShaftSizingMargin.Bottom);
			Rectangle iB = new Rectangle(imgLeft + mShaftSizingMargin.Left, imgTop + imgHeight - mShaftSizingMargin.Bottom, imgWidth - mShaftSizingMargin.Horizontal, mShaftSizingMargin.Bottom);
			Rectangle iRB = new Rectangle(imgLeft + imgWidth - mShaftSizingMargin.Right, imgTop + imgHeight - mShaftSizingMargin.Bottom, mShaftSizingMargin.Right, mShaftSizingMargin.Bottom);
			#endregion

			Rectangle tLT = new Rectangle();
			Rectangle tT = new Rectangle();
			Rectangle tRT = new Rectangle();
			Rectangle tLM = new Rectangle();
			Rectangle tM = new Rectangle();
			Rectangle tRM = new Rectangle();
			Rectangle tLB = new Rectangle();
			Rectangle tB = new Rectangle();
			Rectangle tRB = new Rectangle();

			int offset = 0;
			//shaftFirstHoriz;
			//shaftSecondHoriz;
			//shaftFirstVert;
			//shaftSecondVert;
			int shaftFirstH = 0;
			int shaftFirstV = 0;
			int height;
			int width;
			int sh = 0;

			if (orientation == Orientation.Horizontal)
			{
				if (ScrollV)
				{
					sh = buttonWidth;
				}

				if (!first)
				{
					shaftSecondHoriz = this.ClientRectangle.Width - buttonWidth * 2 - shaftFirstHoriz - thumbHorizWidth - sh;
					width = shaftSecondHoriz;
					offset = buttonWidth + shaftFirstHoriz + thumbHorizWidth; //horizontal offset of the second shaft	
					shaftSecondHorizPos = new Point(offset + this.ClientRectangle.Left, this.ClientRectangle.Bottom - imgHeight);
					
				}
				else
				{
					shaftFirstHoriz = thumbHorizPos.X - buttonWidth - this.ClientRectangle.Left; //width of the first shaft
					width = shaftFirstHoriz;
					offset = buttonWidth;
					shaftFirstHorizPos = new Point(offset + this.ClientRectangle.Left, this.ClientRectangle.Bottom - imgHeight);
					shaftFirstH = shaftFirstHoriz;
				}

				if (shaftFirstH != 0 || !first)
				{
					#region Window parts
					tLT = new Rectangle(offset + this.ClientRectangle.Left, this.ClientRectangle.Bottom - imgHeight, mShaftSizingMarginHoriz.Left, mShaftSizingMarginHoriz.Top);
					tT = new Rectangle(offset + this.ClientRectangle.Left + mShaftSizingMarginHoriz.Left, this.ClientRectangle.Bottom - imgHeight, width - mShaftSizingMarginHoriz.Horizontal, mShaftSizingMarginHoriz.Top);
					tRT = new Rectangle(offset + this.ClientRectangle.Left + width - mShaftSizingMarginHoriz.Right, this.ClientRectangle.Bottom - imgHeight, mShaftSizingMarginHoriz.Right, mShaftSizingMarginHoriz.Top);

					tLM = new Rectangle(offset + this.ClientRectangle.Left , this.ClientRectangle.Bottom - imgHeight + mShaftSizingMarginHoriz.Top, mShaftSizingMarginHoriz.Left, imgHeight - mShaftSizingMarginHoriz.Vertical);
					tM = new Rectangle(offset + this.ClientRectangle.Left + mShaftSizingMarginHoriz.Left, this.ClientRectangle.Bottom - imgHeight + mShaftSizingMarginHoriz.Top, width - mShaftSizingMarginHoriz.Horizontal, imgHeight - mShaftSizingMarginHoriz.Vertical);
					tRM = new Rectangle(offset + this.ClientRectangle.Left + width - mShaftSizingMarginHoriz.Right, this.ClientRectangle.Bottom - imgHeight + mShaftSizingMarginHoriz.Top, mShaftSizingMarginHoriz.Right, imgHeight - mShaftSizingMarginHoriz.Vertical);

					tLB = new Rectangle(offset + this.ClientRectangle.Left, this.ClientRectangle.Bottom - mShaftSizingMarginHoriz.Bottom, mShaftSizingMarginHoriz.Left, mShaftSizingMarginHoriz.Bottom);
					tB = new Rectangle(offset + this.ClientRectangle.Left + mShaftSizingMarginHoriz.Left, this.ClientRectangle.Bottom - mShaftSizingMarginHoriz.Bottom, width - mShaftSizingMarginHoriz.Horizontal, mShaftSizingMarginHoriz.Bottom);
					tRB = new Rectangle(offset + this.ClientRectangle.Left + width - mShaftSizingMarginHoriz.Right, this.ClientRectangle.Bottom - mShaftSizingMarginHoriz.Bottom, mShaftSizingMarginHoriz.Right, mShaftSizingMarginHoriz.Bottom);
					#endregion
				}

				
			}
			else
			{
				if (ScrollH)
				{
					sh = buttonHeight;
				}

				if (!first)
				{
					shaftSecondVert = this.ClientRectangle.Height - buttonHeight * 2 - shaftFirstVert - thumbVertHeight - sh;
					height = shaftSecondVert;
					offset = buttonHeight + shaftFirstVert + thumbVertHeight; // vertical offset of the second shaft
					shaftSecondVertPos = new Point(this.ClientRectangle.Right - imgWidth, offset + this.ClientRectangle.Top);
				}
				else
				{
					shaftFirstVert = thumbVertPos.Y - buttonHeight - this.ClientRectangle.Top; //height of the first shaft
					height = shaftFirstVert;
					offset = buttonHeight;
					shaftFirstVertPos = new Point(this.ClientRectangle.Right - imgWidth, offset + this.ClientRectangle.Top);
					shaftFirstV = shaftFirstVert;
				}

				if (shaftFirstV != 0 || !first)
				{
					#region window parts
					tLT = new Rectangle(this.ClientRectangle.Right - imgWidth, offset + this.ClientRectangle.Top, mShaftSizingMarginVert.Left, mShaftSizingMarginVert.Top);
					tT = new Rectangle(this.ClientRectangle.Right - imgWidth + mShaftSizingMarginVert.Left, offset + this.ClientRectangle.Top, imgWidth - mShaftSizingMarginVert.Horizontal, mShaftSizingMarginVert.Top);
					tRT = new Rectangle(this.ClientRectangle.Right - mShaftSizingMarginVert.Right, offset + this.ClientRectangle.Top, mShaftSizingMarginVert.Right, mShaftSizingMarginVert.Top);

					tLM = new Rectangle(this.ClientRectangle.Right - imgWidth, offset + this.ClientRectangle.Top + mShaftSizingMarginVert.Top, mShaftSizingMarginVert.Left, height - mShaftSizingMarginVert.Bottom);
					tM = new Rectangle(this.ClientRectangle.Right - imgWidth + mShaftSizingMarginVert.Left, offset + this.ClientRectangle.Top + mShaftSizingMarginVert.Top, imgWidth - mShaftSizingMarginVert.Horizontal, height - mShaftSizingMarginVert.Bottom);
					tRM = new Rectangle(this.ClientRectangle.Right - mShaftSizingMarginVert.Right, offset + this.ClientRectangle.Top + mShaftSizingMarginVert.Top, mShaftSizingMarginVert.Right, height - mShaftSizingMarginVert.Bottom);

					tLB = new Rectangle(this.ClientRectangle.Right - imgWidth, offset + this.ClientRectangle.Top /*+ mShaftSizingMarginVert.Top*/ + height - mShaftSizingMarginVert.Bottom, mShaftSizingMarginVert.Left, mShaftSizingMarginVert.Bottom);
					tB = new Rectangle(this.ClientRectangle.Right - imgWidth + mShaftSizingMarginVert.Left, offset + this.ClientRectangle.Top /*+ mShaftSizingMarginVert.Top*/ + height - mShaftSizingMarginVert.Bottom, imgWidth - mShaftSizingMarginVert.Horizontal, mShaftSizingMarginVert.Bottom);
					tRB = new Rectangle(this.ClientRectangle.Right - mShaftSizingMarginVert.Right, offset + this.ClientRectangle.Top /*+ mShaftSizingMarginVert.Top*/ + height - mShaftSizingMarginVert.Bottom, mShaftSizingMarginVert.Right, mShaftSizingMarginVert.Bottom);
					#endregion
				}
			}

			#region Drawing
			e.Graphics.DrawImage(img, tLT, iLT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tT, iT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRT, iRT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLM, iLM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tM, iM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRM, iRM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLB, iLB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tB, iB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRB, iRB, GraphicsUnit.Pixel);
			#endregion

			shaftFirstH = 0;
			shaftFirstV = 0;
		}

		private void DrawButton(PaintEventArgs e/*, int shaftWidth, int shaftHeight*/, bool first, Orientation orientation)
		{
			System.Drawing.Image img = Skin.GetImageFromBitmapResource(Skin.KeyPrefix + "common.scrollbutton.png");
			bool verticalLayout = false;
			if (Skin.SelectSingleNode("Elements/ScrollButton/Image").Attributes["Layout"] != null)
			{
				if (Skin.SelectSingleNode("Elements/ScrollButton/Image").Attributes["Layout"].Value.ToLower() == "vertical")
				{
					verticalLayout = true;
				}
			}
			//int imgCount = 16;
			int imgCount = 0;
			if (Skin.SelectSingleNode("Elements/ScrollButton/Image").Attributes["ImageCount"] != null)
			{
				imgCount = int.Parse(Skin.SelectSingleNode("Elements/ScrollButton/Image").Attributes["ImageCount"].Value);
			}

			int imgWidth;
			int imgHeight;
			int imgLeft = 0;
			int imgTop = 0;

			int d = (orientation == Orientation.Horizontal) ? ORIENTATION_OFFSET : 0;
			int r = first ? 0 : IMAGE_OFFSET;

			if (!verticalLayout)
			{
				imgWidth = (Int32)(img.Width / imgCount);
				imgHeight = img.Height - 1;

				if (!this.Enabled)
				{
					imgLeft = 3 * imgWidth;
				}
				else
				{
					if (first) //first button
					{
						mMState = (orientation == Orientation.Horizontal) ? mMStateButtonFirstHoriz : mMStateButtonFirstVert;
						switch (mMState)
						{
							default:
							case MouseEventState.None:
								imgLeft = (d + r + 0) * imgHeight;
								break;
							case MouseEventState.OnHover:
								imgLeft = (d + r + 1) * imgWidth;
								break;
							case MouseEventState.OnDown:
								imgLeft = (d + r + 2) * imgWidth;
								break;
							
						}
					}
					else // second button
					{
						mMState = (orientation == Orientation.Horizontal) ? mMStateButtonSecondHoriz : mMStateButtonSecondVert;

						switch (mMState)
						{
							default:
							case MouseEventState.None:
								imgLeft = (d + r + 0) * imgHeight;
								break;
							case MouseEventState.OnHover:
								imgLeft = (d + r + 1) * imgWidth;
								break;
							case MouseEventState.OnDown:
								imgLeft = (d + r + 2) * imgWidth;
								break;
							
						}
					}
				}
			}
			else
			{
				imgWidth = img.Width;
				imgHeight = (Int32)(img.Height / imgCount);

				if (!this.Enabled)
				{
					imgTop = 3 * imgHeight;
				}
				else
				{
					if (first) // first button
					{
						mMState = (orientation == Orientation.Horizontal) ? mMStateButtonFirstHoriz : mMStateButtonFirstVert;
						switch (mMState)
						{
							default:
							case MouseEventState.None:
								imgTop = (d + r + 0) * imgHeight;
								break;
							case MouseEventState.OnHover:
								imgTop = (d + r + 1) * imgHeight;
								break;
							case MouseEventState.OnDown:
								imgTop = (d + r + 2) * imgHeight;
								break;
						
						}
					}
					else //second button
					{
						mMState = (orientation == Orientation.Horizontal) ? mMStateButtonSecondHoriz : mMStateButtonSecondVert;
						switch (mMState)
						{
							default:
							case MouseEventState.None:
								imgTop = (d + r + 0) * imgHeight;
								break;
							case MouseEventState.OnHover:
								imgTop = (d + r + 1) * imgHeight;
								break;
							case MouseEventState.OnDown:
								imgTop = (d + r + 2) * imgHeight;
								break;
							
						}
					}
				}
			}

			#region Image parts
			Rectangle iLT = new Rectangle(imgLeft, imgTop, mButtonSizingMargin.Left, mButtonSizingMargin.Top);
			Rectangle iT = new Rectangle(imgLeft + mButtonSizingMargin.Left, imgTop, imgWidth - mButtonSizingMargin.Horizontal, mButtonSizingMargin.Top);
			Rectangle iRT = new Rectangle(imgLeft + imgWidth - mButtonSizingMargin.Right, imgTop, mButtonSizingMargin.Right, mButtonSizingMargin.Top);
			Rectangle iLM = new Rectangle(imgLeft, imgTop + mButtonSizingMargin.Top, mButtonSizingMargin.Left, imgHeight - mButtonSizingMargin.Vertical);
			Rectangle iM = new Rectangle(imgLeft + mButtonSizingMargin.Left, imgTop + mButtonSizingMargin.Top, imgWidth - mButtonSizingMargin.Horizontal, imgHeight - mButtonSizingMargin.Vertical);
			Rectangle iRM = new Rectangle(imgLeft + imgWidth - mButtonSizingMargin.Right, imgTop + mButtonSizingMargin.Top, mButtonSizingMargin.Right, imgHeight - mButtonSizingMargin.Vertical);
			Rectangle iLB = new Rectangle(imgLeft, imgTop + imgHeight - mButtonSizingMargin.Bottom, mButtonSizingMargin.Left, mButtonSizingMargin.Bottom);
			Rectangle iB = new Rectangle(imgLeft + mButtonSizingMargin.Left, imgTop + imgHeight - mButtonSizingMargin.Bottom, imgWidth - mButtonSizingMargin.Horizontal, mButtonSizingMargin.Bottom);
			Rectangle iRB = new Rectangle(imgLeft + imgWidth - mButtonSizingMargin.Right, imgTop + imgHeight - mButtonSizingMargin.Bottom, mButtonSizingMargin.Right, mButtonSizingMargin.Bottom);
			#endregion

			Rectangle tLT;
			Rectangle tT;
			Rectangle tRT;
			Rectangle tLM;
			Rectangle tM;
			Rectangle tRM;
			Rectangle tLB;
			Rectangle tB;
			Rectangle tRB;

			int offset = 0;
			int glyphPosX = 0;
			int glyphPosY = 0;

			if (orientation == Orientation.Horizontal)
			{
				if (!first)
				{
					//horizontal offset of the second button
					if (ScrollV)
					{
						offset = this.ClientRectangle.Width - buttonWidth * 2;
					}
					else
					{
						offset = this.ClientRectangle.Width - buttonWidth;
					}
					buttonSecondHorizPos = new Point(offset + this.ClientRectangle.Left, this.ClientRectangle.Bottom - imgHeight);
				}
				else
				{
					buttonFirstHorizPos = new Point(offset + this.ClientRectangle.Left, this.ClientRectangle.Bottom - imgHeight);
				}

				tLT = new Rectangle(offset + this.ClientRectangle.Left, this.ClientRectangle.Bottom - imgHeight, mButtonSizingMargin.Left, mButtonSizingMargin.Top);
				tT = new Rectangle(offset + this.ClientRectangle.Left + mButtonSizingMargin.Left, this.ClientRectangle.Bottom - imgHeight, imgWidth - mButtonSizingMargin.Horizontal, mButtonSizingMargin.Top);
				tRT = new Rectangle(offset + this.ClientRectangle.Left + imgWidth - mButtonSizingMargin.Right, this.ClientRectangle.Bottom - imgHeight, mButtonSizingMargin.Right, mButtonSizingMargin.Top);

				tLM = new Rectangle(offset + this.ClientRectangle.Left, this.ClientRectangle.Bottom - imgHeight + mButtonSizingMargin.Top, mButtonSizingMargin.Left, imgHeight - mButtonSizingMargin.Vertical);
				tM = new Rectangle(offset + this.ClientRectangle.Left + mButtonSizingMargin.Left, this.ClientRectangle.Bottom - imgHeight + mButtonSizingMargin.Top, imgWidth - mButtonSizingMargin.Horizontal, imgHeight - mButtonSizingMargin.Vertical);
				tRM = new Rectangle(offset + this.ClientRectangle.Left + imgWidth - mButtonSizingMargin.Right, this.ClientRectangle.Bottom - imgHeight + mButtonSizingMargin.Top, mButtonSizingMargin.Right, imgHeight - mButtonSizingMargin.Vertical);

				tLB = new Rectangle(offset + this.ClientRectangle.Left, this.ClientRectangle.Bottom - mButtonSizingMargin.Bottom, mButtonSizingMargin.Left, mButtonSizingMargin.Bottom);
				tB = new Rectangle(offset + this.ClientRectangle.Left + mButtonSizingMargin.Left, this.ClientRectangle.Bottom - mButtonSizingMargin.Bottom, imgWidth - mButtonSizingMargin.Horizontal, mButtonSizingMargin.Bottom);
				tRB = new Rectangle(offset + this.ClientRectangle.Left + imgWidth - mButtonSizingMargin.Right, this.ClientRectangle.Bottom - mButtonSizingMargin.Bottom, mButtonSizingMargin.Right, mButtonSizingMargin.Bottom);

				glyphPosX = this.ClientRectangle.Left + offset;
				glyphPosY = this.ClientRectangle.Bottom - imgHeight;
			}
			else
			{
				if (!first)
				{
					//vertical offset of the second button
					if (ScrollH)
					{
						offset = this.ClientRectangle.Height - buttonHeight * 2;
					}
					else
					{
						offset = this.ClientRectangle.Height - buttonHeight;
					}
					buttonSecondVertPos = new Point(this.ClientRectangle.Right - imgWidth, offset + this.ClientRectangle.Top);
				}
				else
				{
					buttonFirstVertPos = new Point(this.ClientRectangle.Right - imgWidth, offset + this.ClientRectangle.Top);
				}

				tLT = new Rectangle(this.ClientRectangle.Right - imgWidth, offset + this.ClientRectangle.Top, mButtonSizingMargin.Left, mButtonSizingMargin.Top);
				tT = new Rectangle(this.ClientRectangle.Right - imgWidth + mButtonSizingMargin.Left, offset + this.ClientRectangle.Top, imgWidth - mButtonSizingMargin.Horizontal, mButtonSizingMargin.Top);
				tRT = new Rectangle(this.ClientRectangle.Right - mButtonSizingMargin.Right, offset + this.ClientRectangle.Top, mButtonSizingMargin.Right, mButtonSizingMargin.Top);

				tLM = new Rectangle(this.ClientRectangle.Right - imgWidth, offset + this.ClientRectangle.Top + mButtonSizingMargin.Top, mButtonSizingMargin.Left, imgHeight - mButtonSizingMargin.Vertical);
				tM = new Rectangle(this.ClientRectangle.Right - imgWidth + mButtonSizingMargin.Left, offset + this.ClientRectangle.Top + mButtonSizingMargin.Top, imgWidth - mButtonSizingMargin.Horizontal, imgHeight - mButtonSizingMargin.Vertical);
				tRM = new Rectangle(this.ClientRectangle.Right - mButtonSizingMargin.Right, offset + this.ClientRectangle.Top + mButtonSizingMargin.Top, mButtonSizingMargin.Right, imgHeight - mButtonSizingMargin.Vertical);

				tLB = new Rectangle(this.ClientRectangle.Right - imgWidth, offset + this.ClientRectangle.Top + imgHeight - mButtonSizingMargin.Bottom, mButtonSizingMargin.Left, mButtonSizingMargin.Bottom);
				tB = new Rectangle(this.ClientRectangle.Right - imgWidth + mButtonSizingMargin.Left, offset + this.ClientRectangle.Top + imgHeight - mButtonSizingMargin.Bottom, imgWidth - mButtonSizingMargin.Horizontal, mButtonSizingMargin.Bottom);
				tRB = new Rectangle(this.ClientRectangle.Right - mButtonSizingMargin.Right, offset + this.ClientRectangle.Top + imgHeight - mButtonSizingMargin.Bottom, mButtonSizingMargin.Right, mButtonSizingMargin.Bottom);

				glyphPosX = this.ClientRectangle.Right - imgWidth;
				glyphPosY = this.ClientRectangle.Top + offset;
			}

			buttonWidth = imgWidth;
			buttonHeight = imgHeight;

			#region Drawing
			e.Graphics.DrawImage(img, tLT, iLT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tT, iT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRT, iRT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLM, iLM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tM, iM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRM, iRM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLB, iLB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tB, iB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRB, iRB, GraphicsUnit.Pixel);
			#endregion

			DrawGlyph(e, imgWidth, imgHeight, glyphPosX, glyphPosY, orientation, d, r, false);
		}

		private void DrawThumbButton(PaintEventArgs e, Orientation orientation)
		{
			System.Drawing.Image img;
			bool verticalLayout;
			if (orientation == Orientation.Horizontal)
			{
				img = Skin.GetImageFromBitmapResource(Skin.KeyPrefix + "common.scrollthumbbutton.png");
				verticalLayout = false;
				if (Skin.SelectSingleNode("Elements/ScrollThumbButton/Image").Attributes["Layout"] != null)
				{
					if (Skin.SelectSingleNode("Elements/ScrollThumbButton/Image").Attributes["Layout"].Value.ToLower() == "vertical")
					{
						verticalLayout = true;
					}
				}
			}
			else
			{
				img = Skin.GetImageFromBitmapResource(Skin.KeyPrefix + "common.scrollthumbbuttonvert.png");
				verticalLayout = false;
				if (Skin.SelectSingleNode("Elements/ScrollThumbButtonVert/Image").Attributes["Layout"] != null)
				{
					if (Skin.SelectSingleNode("Elements/ScrollThumbButtonVert/Image").Attributes["Layout"].Value.ToLower() == "vertical")
					{
						verticalLayout = true;
					}
				}
			}
			//int imgCount = 4;
			int imgCount = 0;
			if (Skin.SelectSingleNode("Elements/ScrollThumbButtonVert/Image").Attributes["ImageCount"] != null)
			{
				imgCount = int.Parse(Skin.SelectSingleNode("Elements/ScrollThumbButtonVert/Image").Attributes["ImageCount"].Value);
			}
			int imgWidth;
			int imgHeight;
			int imgLeft = 0;
			int imgTop = 0;


			mMState = (orientation == Orientation.Horizontal) ? mMStateThumbHoriz: mMStateThumbVert;

			if (!verticalLayout)
			{
				imgWidth = (Int32)(img.Width / imgCount);
				imgHeight = img.Height - 1;

				if (!this.Enabled)
				{
					imgLeft = 3 * imgWidth;
				}
				else
				{
					switch (mMState)
					{
						default:
						case MouseEventState.None:
							imgLeft = 0;
							break;
						case MouseEventState.OnHover:
							imgLeft = 1 * imgWidth;
							break;
						case MouseEventState.OnDown:
							imgLeft = 2 * imgWidth;
							break;
					}
				}
			}
			else
			{
				imgWidth = img.Width;
				imgHeight = (Int32)(img.Height / imgCount);

				if (!this.Enabled)
				{
					imgTop = 3 * imgHeight;
				}
				else
				{
					switch (mMState)
					{
						default:
						case MouseEventState.None:
							imgTop = 0;
							break;
						case MouseEventState.OnHover:
							imgTop = 1 * imgHeight;
							break;
						case MouseEventState.OnDown:
							imgTop = 2 * imgHeight;
							break;
					}
				}
			}

			Margin mThumbSizingMargin = (orientation == Orientation.Horizontal) ? mThumbSizingMarginHoriz : mThumbSizingMarginVert;

			#region Image parts
			Rectangle iLT = new Rectangle(imgLeft, imgTop, mThumbSizingMargin.Left, mThumbSizingMargin.Top);
			Rectangle iT = new Rectangle(imgLeft + mThumbSizingMargin.Left, imgTop, imgWidth - mThumbSizingMargin.Horizontal, mThumbSizingMargin.Top);
			Rectangle iRT = new Rectangle(imgLeft + imgWidth - mThumbSizingMargin.Right, imgTop, mThumbSizingMargin.Right, mThumbSizingMargin.Top);
			Rectangle iLM = new Rectangle(imgLeft, imgTop + mThumbSizingMargin.Top, mThumbSizingMargin.Left, imgHeight - mThumbSizingMargin.Vertical);
			Rectangle iM = new Rectangle(imgLeft + mThumbSizingMargin.Left, imgTop + mThumbSizingMargin.Top, imgWidth - mThumbSizingMargin.Horizontal, imgHeight - mThumbSizingMargin.Vertical);
			Rectangle iRM = new Rectangle(imgLeft + imgWidth - mThumbSizingMargin.Right, imgTop + mThumbSizingMargin.Top, mThumbSizingMargin.Right, imgHeight - mThumbSizingMargin.Vertical);
			Rectangle iLB = new Rectangle(imgLeft, imgTop + imgHeight - mThumbSizingMargin.Bottom, mThumbSizingMargin.Left, mThumbSizingMargin.Bottom);
			Rectangle iB = new Rectangle(imgLeft + mThumbSizingMargin.Left, imgTop + imgHeight - mThumbSizingMargin.Bottom, imgWidth - mThumbSizingMargin.Horizontal, mThumbSizingMargin.Bottom);
			Rectangle iRB = new Rectangle(imgLeft + imgWidth - mThumbSizingMargin.Right, imgTop + imgHeight - mThumbSizingMargin.Bottom, mThumbSizingMargin.Right, mThumbSizingMargin.Bottom);
			#endregion

			Rectangle tLT = new Rectangle();
			Rectangle tT = new Rectangle();
			Rectangle tRT = new Rectangle();
			Rectangle tLM = new Rectangle();
			Rectangle tM = new Rectangle();
			Rectangle tRM = new Rectangle();
			Rectangle tLB = new Rectangle();
			Rectangle tB = new Rectangle();
			Rectangle tRB = new Rectangle();


			double rate;
			int shaftWidth;
			int shaftHeight;
			int offset;
			
			int thumbWidth = imgWidth;
			int thumbHeight = imgHeight;

			//thumbHorizWidth ;
			//thumbHorizHeight;
			//thumbVertWidth;
			//thumbVertHeight;
			Point thumbPos = new Point();
			

			switch (orientation)
			{
				case Orientation.Horizontal:
					
					int i = (ScrollV) ? 3 : 2;
					shaftWidth = this.ClientRectangle.Width - buttonWidth * i; 	
					rate = (double)shaftWidth / (this.innerPanel.HorizontalScroll.Maximum - this.innerPanel.HorizontalScroll.Minimum);
					//val = rate * ValueH;
					//thumbHorizWidth = (int)Math.Round((double)(shaftWidth * rate));
					thumbHorizWidth = Convert.ToInt32(shaftWidth * rate) ;
					
					//offset = (int)(buttonWidth + val);
					//offset =(mMovedThumbHorizOffset == 0)? buttonWidth : mMovedThumbHorizOffset ;
					offset = mMovedThumbHorizOffset + buttonWidth;

					tLT = new Rectangle(offset + this.ClientRectangle.Left, this.ClientRectangle.Bottom - imgHeight, mThumbSizingMarginHoriz.Left, mThumbSizingMarginHoriz.Top);
					tT = new Rectangle(offset + this.ClientRectangle.Left + mThumbSizingMarginHoriz.Left, this.ClientRectangle.Bottom - imgHeight, thumbHorizWidth - mThumbSizingMarginHoriz.Horizontal, mThumbSizingMarginHoriz.Top);
					tRT = new Rectangle(offset + this.ClientRectangle.Left + thumbHorizWidth - mThumbSizingMarginHoriz.Right, this.ClientRectangle.Bottom - imgHeight, mThumbSizingMarginHoriz.Right, mThumbSizingMarginHoriz.Top);

					tLM = new Rectangle(offset + this.ClientRectangle.Left, this.ClientRectangle.Bottom - imgHeight + mThumbSizingMarginHoriz.Top, mThumbSizingMarginHoriz.Left, imgHeight - mThumbSizingMarginHoriz.Vertical);
					tM = new Rectangle(offset + this.ClientRectangle.Left + mThumbSizingMarginHoriz.Left, this.ClientRectangle.Bottom - imgHeight + mThumbSizingMarginHoriz.Top, thumbHorizWidth - mThumbSizingMarginHoriz.Horizontal, imgHeight - mThumbSizingMarginHoriz.Vertical);
					tRM = new Rectangle(offset + this.ClientRectangle.Left + thumbHorizWidth - mThumbSizingMarginHoriz.Right, this.ClientRectangle.Bottom - imgHeight + mThumbSizingMarginHoriz.Top, mThumbSizingMarginHoriz.Right, imgHeight - mThumbSizingMarginHoriz.Vertical);

					tLB = new Rectangle(offset + this.ClientRectangle.Left, this.ClientRectangle.Bottom - mThumbSizingMarginHoriz.Bottom, mThumbSizingMarginHoriz.Left, mThumbSizingMarginHoriz.Bottom);
					tB = new Rectangle(offset + this.ClientRectangle.Left + mThumbSizingMarginHoriz.Left, this.ClientRectangle.Bottom - mThumbSizingMarginHoriz.Bottom, thumbHorizWidth - mThumbSizingMarginHoriz.Horizontal, mThumbSizingMarginHoriz.Bottom);
					tRB = new Rectangle(offset + this.ClientRectangle.Left + thumbHorizWidth - mThumbSizingMarginHoriz.Right, this.ClientRectangle.Bottom - mThumbSizingMarginHoriz.Bottom, mThumbSizingMarginHoriz.Right, mThumbSizingMarginHoriz.Bottom);

					thumbHorizPos = new Point();
					thumbHorizPos.X = ClientRectangle.Left + offset; // the x coordiante of the thumb button's left upper corner 
					thumbHorizPos.Y = ClientRectangle.Bottom - imgHeight; // the y coordinate of the thumb button's left upper corner
					thumbPos = thumbHorizPos;
					thumbWidth = thumbHorizWidth;
					
					break;

				case Orientation.Vertical:

					i = (ScrollH) ? 3 : 2;
					shaftHeight = this.ClientRectangle.Height - buttonHeight * i;
					rate = (double)shaftHeight / (this.innerPanel.HorizontalScroll.Maximum - this.innerPanel.HorizontalScroll.Minimum);
					//val = rate * ValueH;
					//thumbHorizWidth = (int)Math.Round((double)(shaftWidth * rate));
					thumbVertHeight = Convert.ToInt32(shaftHeight * rate);

					//offset = (int)(buttonWidth + val);
					//offset = (mMovedThumbVertOffset == 0) ? buttonHeight : mMovedThumbVertOffset;
					offset = mMovedThumbVertOffset + buttonHeight;
					
					tLT = new Rectangle(this.ClientRectangle.Right - imgWidth, offset + this.ClientRectangle.Top, mThumbSizingMarginVert.Left, mThumbSizingMarginVert.Top);
					tT = new Rectangle(this.ClientRectangle.Right - imgWidth + mThumbSizingMarginVert.Left, offset + this.ClientRectangle.Top, imgWidth - mThumbSizingMarginVert.Horizontal, mThumbSizingMarginVert.Top);
					tRT = new Rectangle(this.ClientRectangle.Right - mThumbSizingMarginVert.Right, offset + this.ClientRectangle.Top, mThumbSizingMarginVert.Right, mThumbSizingMarginVert.Top);

					tLM = new Rectangle(this.ClientRectangle.Right - imgWidth, offset + this.ClientRectangle.Top + mThumbSizingMarginVert.Top, mThumbSizingMarginVert.Left, thumbVertHeight - mThumbSizingMarginVert.Vertical);
					tM = new Rectangle(this.ClientRectangle.Right - imgWidth + mThumbSizingMarginVert.Left, offset + this.ClientRectangle.Top + mThumbSizingMarginVert.Top, imgWidth - mThumbSizingMarginVert.Horizontal, thumbVertHeight - mThumbSizingMarginVert.Vertical);
					tRM = new Rectangle(this.ClientRectangle.Right - mThumbSizingMarginVert.Right, offset + this.ClientRectangle.Top + mThumbSizingMarginVert.Top, mThumbSizingMarginVert.Right, thumbVertHeight - mThumbSizingMarginVert.Vertical);

					tLB = new Rectangle(this.ClientRectangle.Right - imgWidth, offset + this.ClientRectangle.Top + thumbVertHeight - mThumbSizingMarginVert.Bottom, mThumbSizingMarginVert.Left, mThumbSizingMarginVert.Bottom);
					tB = new Rectangle(this.ClientRectangle.Right - imgWidth + mThumbSizingMarginVert.Left, offset + this.ClientRectangle.Top + thumbVertHeight - mThumbSizingMarginVert.Bottom, imgWidth - mThumbSizingMarginVert.Horizontal, mThumbSizingMarginVert.Bottom);
					tRB = new Rectangle(this.ClientRectangle.Right - mThumbSizingMarginVert.Right, offset + this.ClientRectangle.Top + thumbVertHeight - mThumbSizingMarginVert.Bottom, mThumbSizingMarginVert.Right, mThumbSizingMarginVert.Bottom);

					thumbVertPos = new Point();
					thumbVertPos.X = this.ClientRectangle.Right - imgWidth; // the x coordiante of the thumb button's left upper corner 
					thumbVertPos.Y = this.ClientRectangle.Top + offset; // the y coordinate of the thumb button's left upper corner
					thumbPos = thumbVertPos;
					thumbHeight = thumbVertHeight;
					break;
			}

			#region Drawing
			e.Graphics.DrawImage(img, tLT, iLT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tT, iT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRT, iRT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLM, iLM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tM, iM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRM, iRM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLB, iLB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tB, iB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRB, iRB, GraphicsUnit.Pixel);
			#endregion

			DrawGlyph(e, thumbWidth, thumbHeight, thumbPos.X, thumbPos.Y, orientation, 0, 0, true);
		}

		private void DrawGlyph(PaintEventArgs e, int buttonWidth, int buttonHeight, int xPosition, int yPosition, Orientation orientation, int d, int r, bool thumb)
		{
			System.Drawing.Image img;
			bool verticalLayout;

			int imgCount = 0;

			if (thumb)
			{
				img = Skin.GetImageFromBitmapResource(Skin.KeyPrefix + "common.scrollthumbbutton_glyph.png");
				verticalLayout = false;
				if (orientation == Orientation.Horizontal)
				{
					if (Skin.SelectSingleNode("Elements/ScrollThumbButton/Glyph").Attributes["Layout"] != null)
					{
						if (Skin.SelectSingleNode("Elements/ScrollThumbButton/Glyph").Attributes["Layout"].Value.ToLower() == "vertical")
						{
							verticalLayout = true;
						}
					}
					if (Skin.SelectSingleNode("Elements/ScrollThumbButton/Glyph").Attributes["ImageCount"] != null)
					{
						imgCount = int.Parse(Skin.SelectSingleNode("Elements/ScrollThumbButton/Glyph").Attributes["ImageCount"].Value);
					}
				}
				else
				{
					if (Skin.SelectSingleNode("Elements/ScrollThumbButtonVert/Glyph").Attributes["Layout"] != null)
					{
						if (Skin.SelectSingleNode("Elements/ScrollThumbButtonVert/Glyph").Attributes["Layout"].Value.ToLower() == "vertical")
						{
							verticalLayout = true;
						}
					}
					if (Skin.SelectSingleNode("Elements/ScrollThumbButtonVert/Glyph").Attributes["ImageCount"] != null)
					{
						imgCount = int.Parse(Skin.SelectSingleNode("Elements/ScrollThumbButtonVert/Glyph").Attributes["ImageCount"].Value);
					}
				}
				//imgCount = 4;
				
			}
			else
			{
				img = Skin.GetImageFromBitmapResource(Skin.KeyPrefix + "common.scrollbutton_glyph.png");
				verticalLayout = false;
				if (Skin.SelectSingleNode("Elements/ScrollButton/Glyph").Attributes["Layout"] != null)
				{
					if (Skin.SelectSingleNode("Elements/ScrollButton/Glyph").Attributes["Layout"].Value.ToLower() == "vertical")
					{
						verticalLayout = true;
					}
				}

				//imgCount = 16;
				if (Skin.SelectSingleNode("Elements/ScrollButton/Glyph").Attributes["ImageCount"] != null)
				{
					imgCount = int.Parse(Skin.SelectSingleNode("Elements/ScrollButton/Glyph").Attributes["ImageCount"].Value);		
				}
				
			}
			
			int imgWidth;
			int imgHeight;
			int imgLeft = 0;
			int imgTop = 0;

			if (!verticalLayout)
			{
				imgWidth = (Int32)(img.Width / imgCount);
				imgHeight = img.Height - 1;

				if (!this.Enabled)
				{
					imgLeft = 3 * imgWidth;
				}
				else
				{
					switch (mMState)
					{
						default:
						case MouseEventState.None:
							imgLeft = (d + r + 0) * imgWidth;
							break;
						case MouseEventState.OnHover:
							imgLeft = (d + r + 1) * imgWidth;
							break;
						case MouseEventState.OnDown:
							imgLeft = (d + r + 2) * imgWidth;
							break;
					}
				}
			}
			else
			{
				imgWidth = img.Width;
				imgHeight = (Int32)(img.Height / imgCount);

				if (!this.Enabled)
				{
					imgTop = 3 * imgHeight;
				}
				else
				{
					switch (mMState)
					{
						default:
						case MouseEventState.None:
							imgTop = (d + r + 0) * imgHeight;
							break;
						case MouseEventState.OnHover:
							imgTop = (d + r + 1) * imgHeight;
							break;
						case MouseEventState.OnDown:
							imgTop = (d + r + 2) * imgHeight;
							break;
					}
				}
			}

			Rectangle src = new Rectangle(imgLeft, imgTop, imgWidth, imgHeight);
			Rectangle dest = new Rectangle(xPosition + (int)Math.Round((buttonWidth - imgWidth) / 2.0), yPosition + (int)Math.Round((buttonHeight - imgHeight)/ 2.0), imgWidth, imgHeight);
			e.Graphics.DrawImage(img, dest, src, GraphicsUnit.Pixel);			
		}

		#endregion

		private void MoveThumb(MouseEventArgs e, Orientation orientation, MoveType moveType, int step)
		{
			int pixelRange;
			int realRange;
			int shift = 0;
			double perc;
			
			switch (orientation)
			{
				case Orientation.Horizontal:

					switch (moveType)
					{
						case MoveType.None:
							if (moveType == MoveType.None)
							{
								pixelRange = shaftFirstHoriz + shaftSecondHoriz;
								realRange = this.innerPanel.HorizontalScroll.Maximum - this.innerPanel.HorizontalScroll.Minimum;
								perc = (double)pixelRange / realRange;
								
								shift = e.X - mThumbDownPoint.X;

								mMovedThumbHorizOffset = mThumbDownHorizPos + shift - buttonWidth;
								//mThumbDownHorizPos += mMovedThumbHorizOffset;
								if (mMovedThumbHorizOffset < 0)
								{
									mMovedThumbHorizOffset = 0;
								}
								else
								{
									if (mMovedThumbHorizOffset > pixelRange)
									{
										mMovedThumbHorizOffset = pixelRange;
									}
								}
								ValueH = mMovedThumbHorizOffset;
								
							}
							break;
						case MoveType.Forward:
							pixelRange = shaftFirstHoriz + shaftSecondHoriz;
							ValueH = (this.innerPanel.AutoScrollPosition.X * (-1) >= pixelRange)? pixelRange : this.innerPanel.AutoScrollPosition.X * (-1) + step;
							mMovedThumbHorizOffset = thumbHorizPos.X + step - buttonWidth;
							if (mMovedThumbHorizOffset < 0)
							{
								mMovedThumbHorizOffset = 0;
							}
							else
							{
								
								if (mMovedThumbHorizOffset > pixelRange )
								{
									mMovedThumbHorizOffset = pixelRange;
								}
							}
							break;
						case MoveType.Backward:
							pixelRange = shaftFirstHoriz + shaftSecondHoriz;
							ValueH = (this.innerPanel.AutoScrollPosition.X * (-1) <= 0) ? 0 : this.innerPanel.AutoScrollPosition.X * (-1) - step;
							mMovedThumbHorizOffset = thumbHorizPos.X - step - buttonWidth;
							if (mMovedThumbHorizOffset < 0)
							{
								mMovedThumbHorizOffset = 0;
							}
							else
							{
								
								if (mMovedThumbHorizOffset > pixelRange)
								{
									mMovedThumbHorizOffset = pixelRange;
								}
							}
							break;
					}

					
					break;
				case Orientation.Vertical:
					switch (moveType)
					{
						case MoveType.None:

							pixelRange = shaftFirstVert + shaftSecondVert;
							realRange = this.innerPanel.VerticalScroll.Maximum - this.innerPanel.VerticalScroll.Minimum;
							perc = (double)pixelRange / realRange;
							
							shift = e.Y - mThumbDownPoint.Y;

							mMovedThumbVertOffset =  mThumbDownVertPos + shift - buttonHeight;
							//mThumbDownVertPos += mMovedThumbVertOffset;
							if (mMovedThumbVertOffset < 0)
							{
								mMovedThumbVertOffset = 0;
							}
							else
							{
								if (mMovedThumbVertOffset > pixelRange)
								{
									mMovedThumbVertOffset = pixelRange;
								}
							}
							ValueV = mMovedThumbVertOffset;
							break;
						case MoveType.Forward:
							pixelRange = shaftFirstHoriz + shaftSecondHoriz;
							ValueV = this.innerPanel.AutoScrollPosition.Y * (-1) + step;
							if (ValueV > pixelRange)
							{
								ValueV = pixelRange;
							}
							mMovedThumbVertOffset = thumbVertPos.Y + step - buttonHeight;
							if (mMovedThumbVertOffset < 0)
							{
								mMovedThumbVertOffset = 0;
							}
							else
							{
								if (mMovedThumbVertOffset > pixelRange)
								{
									mMovedThumbVertOffset = pixelRange;
								}
							}
							break;
						case MoveType.Backward:
							pixelRange = shaftFirstVert + shaftSecondVert;
							ValueV = this.innerPanel.AutoScrollPosition.Y * (-1) - step;
							if (ValueV < 0)
							{
								ValueV = 0;
							}
							mMovedThumbVertOffset = thumbVertPos.Y - step - buttonHeight;
							if (mMovedThumbVertOffset < 0)
							{
								mMovedThumbVertOffset = 0;
							}
							else
							{
								if (mMovedThumbVertOffset > pixelRange)
								{
									mMovedThumbVertOffset = pixelRange;
								}
							}
							break;

					}
					
					break;
			}

			this.innerPanel.AutoScrollPosition = new Point((int)ValueH, (int)ValueV);
			this.innerPanel.Invalidate(true);
			this.Invalidate(false);
			Application.DoEvents();
		}

		#region Events

		void SetMouseEventStates(MouseEventArgs e, MouseEventState mouseEventState)
		{
			mMStateButtonFirstHoriz = MouseEventState.None;
			mMStateButtonFirstVert = MouseEventState.None;
			mMStateThumbHoriz = MouseEventState.None;
			mMStateShaftFirstHoriz = MouseEventState.None;
			mMStateShaftSecondHoriz = MouseEventState.None;

			mMStateButtonFirstVert = MouseEventState.None;
			mMStateButtonFirstVert = MouseEventState.None;
			mMStateThumbVert = MouseEventState.None;
			mMStateShaftFirstVert = MouseEventState.None;
			mMStateShaftSecondVert = MouseEventState.None;

			if (ScrollH)
			{
				if (e.X >= buttonFirstHorizPos.X & e.X <= buttonFirstHorizPos.X + buttonWidth & e.Y >= buttonFirstHorizPos.Y & e.Y <= buttonFirstHorizPos.Y + buttonHeight)
				{
					mMStateButtonFirstHoriz = mouseEventState;
					if (mMStateButtonFirstHoriz == MouseEventState.OnDown)
					{
						//mHorizThumbDown = true;
						MoveThumb(null, Orientation.Horizontal, MoveType.Backward, this.innerPanel.HorizontalScroll.SmallChange);
						//while (mHorizThumbDown)
						//{
						//    MoveThumb(null, Orientation.Horizontal, MoveType.Backward, this.innerPanel.HorizontalScroll.SmallChange);
						//}
						//mHorizThumbDown = false;
					}
				}

				if (e.X >= buttonSecondHorizPos.X & e.X <= buttonSecondHorizPos.X + buttonWidth & e.Y >= buttonSecondHorizPos.Y & e.Y <= buttonSecondHorizPos.Y + buttonHeight)
				{
					mMStateButtonSecondHoriz = mouseEventState;
					if (mMStateButtonSecondHoriz == MouseEventState.OnDown)
					{
						//mHorizThumbDown = true;
						MoveThumb(null, Orientation.Horizontal, MoveType.Forward, this.innerPanel.HorizontalScroll.SmallChange);
						//while (mHorizThumbDown)
						//{
						//    MoveThumb(null, Orientation.Horizontal, MoveType.Forward, this.innerPanel.HorizontalScroll.SmallChange);
						//}
						//mHorizThumbDown = false;
					}
				}

				if (e.X >= thumbHorizPos.X & e.X <= thumbHorizPos.X + thumbHorizWidth & e.Y >= thumbHorizPos.Y & e.Y <= thumbHorizPos.Y + thumbHorizHeight)
				{
					mMStateThumbHoriz = mouseEventState;
					if (mMStateThumbHoriz == MouseEventState.OnDown)
					{
						mHorizThumbDown = true;
						mThumbDownHorizPos = this.thumbHorizPos.X;
						mThumbDownPoint.X = e.X;
						mThumbDownPoint.Y = e.Y;
					}
				}

				if (e.X >= shaftFirstHorizPos.X & e.X <= shaftFirstHorizPos.X + shaftFirstHoriz & e.Y >= shaftFirstHorizPos.Y & e.Y <= shaftFirstHorizPos.Y + buttonHeight)
				{
					mMStateShaftFirstHoriz = mouseEventState;
					mHorizThumbDown = true;
					MoveThumb(null, Orientation.Horizontal, MoveType.Backward, this.innerPanel.HorizontalScroll.LargeChange);
				}

				if (e.X >= shaftSecondHorizPos.X & e.X <= shaftSecondHorizPos.X + shaftSecondHoriz & e.Y >= shaftSecondHorizPos.Y & e.Y <= shaftSecondHorizPos.Y + buttonHeight)
				{
					mMStateShaftSecondHoriz = mouseEventState;
					mHorizThumbDown = true;
					MoveThumb(null, Orientation.Horizontal, MoveType.Forward, this.innerPanel.HorizontalScroll.LargeChange);
				}
			}
			if (ScrollV)
			{
				if (e.X >= buttonFirstVertPos.X & e.X <= buttonFirstVertPos.X + buttonWidth & e.Y >= buttonFirstVertPos.Y & e.Y <= buttonFirstVertPos.Y + buttonHeight)
				{
					mMStateButtonFirstVert = mouseEventState;
					if (mMStateButtonFirstVert == MouseEventState.OnDown)
					{
						//mVertThumbDown = true;
						MoveThumb(null, Orientation.Vertical, MoveType.Backward, this.innerPanel.VerticalScroll.SmallChange);
						//while (mVertThumbDown)
						//{
						//    MoveThumb(null, Orientation.Vertical, MoveType.Backward, this.innerPanel.VerticalScroll.SmallChange);
						//}
						//mVertThumbDown = false;
					}
				}

				if (e.X >= buttonSecondVertPos.X & e.X <= buttonSecondVertPos.X + buttonWidth & e.Y >= buttonSecondVertPos.Y & e.Y <= buttonSecondVertPos.Y + buttonHeight)
				{
					mMStateButtonSecondVert = mouseEventState;
					if (mMStateButtonSecondVert == MouseEventState.OnDown)
					{
						//mVertThumbDown = true;
						MoveThumb(null, Orientation.Vertical, MoveType.Forward, this.innerPanel.VerticalScroll.SmallChange);
						//while (mVertThumbDown)
						//{
						//    MoveThumb(null, Orientation.Vertical, MoveType.Forward, this.innerPanel.VerticalScroll.SmallChange);
						//}
						//mVertThumbDown = false;
					}
				}

				if (e.X >= thumbVertPos.X & e.X <= thumbVertPos.X + thumbVertWidth & e.Y >= thumbVertPos.Y & e.Y <= thumbVertPos.Y + thumbVertHeight)
				{
					mMStateThumbVert = mouseEventState;
					if (mMStateThumbVert == MouseEventState.OnDown)
					{
						mVertThumbDown = true;
						mThumbDownVertPos = this.thumbVertPos.Y;
						mThumbDownPoint.X = e.X;
						mThumbDownPoint.Y = e.Y;
					}
				}

				if (e.X >= shaftFirstVertPos.X & e.X <= shaftFirstVertPos.X + buttonWidth & e.Y >= shaftFirstVertPos.Y & e.Y <= shaftFirstVertPos.Y + shaftFirstVert)
				{
					mMStateShaftFirstVert = mouseEventState;
					mVertThumbDown = true;
					MoveThumb(null, Orientation.Vertical, MoveType.Backward, this.innerPanel.VerticalScroll.LargeChange);
				}

				if (e.X >= shaftSecondVertPos.X & e.X <= shaftSecondVertPos.X + buttonWidth & e.Y >= shaftSecondVertPos.Y & e.Y <= shaftSecondVertPos.Y + shaftSecondVert)
				{
					mMStateShaftSecondVert = mouseEventState;
					mVertThumbDown = true;
					MoveThumb(null, Orientation.Vertical, MoveType.Forward, this.innerPanel.VerticalScroll.LargeChange);
				}
			}
			this.Invalidate(true);
		}
		
		void this_MouseEnter(object sender, EventArgs e)
		{
			this.Invalidate();
		}

		void this_MouseMove(object sender, MouseEventArgs e)
		{
			mMStateButtonFirstHoriz = MouseEventState.None;
			mMStateButtonFirstVert = MouseEventState.None;
			mMStateThumbHoriz = MouseEventState.None;
			mMStateShaftFirstHoriz = MouseEventState.None;
			mMStateShaftSecondHoriz = MouseEventState.None;

			mMStateButtonFirstVert = MouseEventState.None;
			mMStateButtonFirstVert = MouseEventState.None;
			mMStateThumbVert = MouseEventState.None;
			mMStateShaftFirstVert = MouseEventState.None;
			mMStateShaftSecondVert = MouseEventState.None;

			bool bH = false;
			bool bV = false;

			if (e.Button == MouseButtons.Left && mHorizThumbDown)
			{
				MoveThumb(e, Orientation.Horizontal, MoveType.None, 0);
				return;
			}

			if (e.Button == MouseButtons.Left && mVertThumbDown)
			{
				MoveThumb(e, Orientation.Vertical, MoveType.None, 0);
				return;
			}

			if (ScrollH)
			{
				if (e.X >= buttonFirstHorizPos.X & e.X <= buttonFirstHorizPos.X + buttonWidth & e.Y >= this.ClientRectangle.Y + buttonFirstHorizPos.Y & e.Y <= this.ClientRectangle.Y + buttonFirstHorizPos.Y + buttonHeight)
				{
					mMStateButtonFirstHoriz = MouseEventState.OnHover;
				}
				else
					if (e.X >= buttonSecondHorizPos.X & e.X <= buttonSecondHorizPos.X + buttonWidth & e.Y >= buttonSecondHorizPos.Y & e.Y <= buttonSecondHorizPos.Y + buttonHeight)
					{
						mMStateButtonSecondHoriz = MouseEventState.OnHover;
					}
					else
						if (e.X >= thumbHorizPos.X & e.X <= thumbHorizPos.X + thumbHorizWidth & e.Y >= thumbHorizPos.Y & e.Y <= thumbHorizPos.Y + thumbHorizHeight)
						{
							if (mHorizThumbDown)
							{
								mMStateThumbHoriz = MouseEventState.OnDown;
							}
							else
							{
								mMStateThumbHoriz = MouseEventState.OnHover;
							}
							
						}
						else
							if (e.X >= shaftFirstHorizPos.X & e.X <= shaftFirstHorizPos.X + shaftFirstHoriz & e.Y >= shaftFirstHorizPos.Y & e.Y <= shaftFirstHorizPos.Y + buttonHeight)
							{
								mMStateShaftFirstHoriz = MouseEventState.OnHover;
							}
							else
								if (e.X >= shaftSecondHorizPos.X & e.X <= shaftSecondHorizPos.X + shaftSecondHoriz & e.Y >= shaftSecondHorizPos.Y & e.Y <= shaftSecondHorizPos.Y + buttonHeight)
								{
									mMStateShaftSecondHoriz = MouseEventState.OnHover;
								}
								else
									bH = true;
				
			}
			if (ScrollV)
			{
				if (e.X >= buttonFirstVertPos.X & e.X <= buttonFirstVertPos.X + buttonWidth & e.Y >= buttonFirstVertPos.Y & e.Y <= buttonFirstVertPos.Y + buttonHeight)
				{
					mMStateButtonFirstVert = MouseEventState.OnHover;
				}
				else
					if (e.X >= buttonSecondVertPos.X & e.X <= buttonSecondVertPos.X + buttonWidth & e.Y >= buttonSecondVertPos.Y & e.Y <= buttonSecondVertPos.Y + buttonHeight)
					{
						mMStateButtonSecondVert = MouseEventState.OnHover;
					}
					else
						if (e.X >= thumbVertPos.X & e.X <= thumbVertPos.X + thumbVertWidth & e.Y >= thumbVertPos.Y & e.Y <= thumbVertPos.Y + thumbVertHeight)
						{
							if (mVertThumbDown)
							{
								mMStateThumbVert = MouseEventState.OnHover;
							}
							else
							{
								mMStateThumbVert = MouseEventState.OnDown;
							}
							
						}
						else
							if (e.X >= shaftFirstVertPos.X & e.X <= shaftFirstVertPos.X + buttonWidth & e.Y >= shaftFirstVertPos.Y & e.Y <= shaftFirstVertPos.Y + shaftFirstVert)
							{
								mMStateShaftFirstVert = MouseEventState.OnHover;
							}
							else
								if (e.X >= shaftSecondVertPos.X & e.X <= shaftSecondVertPos.X + buttonWidth & e.Y >= shaftSecondVertPos.Y & e.Y <= shaftSecondVertPos.Y + shaftSecondVert)
								{
									mMStateShaftSecondVert = MouseEventState.OnHover;
								}
								else
									bV = true;
				
			}

			if (bH & bV)
			{
				SetMouseEventStates(e, MouseEventState.None);
			}

			this.Invalidate();

		}

		void this_MouseUp(object sender, MouseEventArgs e)
		{
			SetMouseEventStates(e, MouseEventState.OnHover);
			mHorizThumbDown = false;
			mVertThumbDown = false;
			mThumbDownPoint.X = 0;
			mThumbDownPoint.Y = 0;
		}

		void this_MouseDown(object sender, MouseEventArgs e)
		{
			SetMouseEventStates(e, MouseEventState.OnDown);
		}

		void this_MouseLeave(object sender, EventArgs e)
		{

			mMStateButtonFirstHoriz = MouseEventState.None;
			mMStateButtonSecondHoriz = MouseEventState.None;
			mMStateThumbHoriz = MouseEventState.None;
			mMStateShaftFirstHoriz = MouseEventState.None;
			mMStateShaftSecondHoriz = MouseEventState.None;

			mMStateButtonFirstVert = MouseEventState.None;
			mMStateButtonSecondVert = MouseEventState.None;
			mMStateThumbVert = MouseEventState.None;
			mMStateShaftFirstVert = MouseEventState.None;
			mMStateShaftSecondVert = MouseEventState.None;
			
			this.Invalidate();
		}

		void this_MouseHover(object sender, EventArgs e)
		{
			this.Invalidate();
		}

		void DefaultLookAndFeel_OnSkinChanged(object sender, EventArgs e)
		{
			InitializeContentSize();
			InitializeControlMargins();
			InitializeInnerPanelSize();
		}

		#endregion

		#region ISkinnable Members

		/// <summary>
		/// Use default skin?
		/// </summary>
		[Category("Look && feel")]
		new public bool UseDefaultSkin
		{
			get
			{
				return mUseDefaultSkin;
			}
			set
			{
				mUseDefaultSkin = value;
				Invalidate();
			}
		}

		/// <summary>
		/// Used skin
		/// </summary>
		[Category("Look && feel")]
		new public Skin Skin
		{
			get
			{
				if (this.UseDefaultSkin)
				{
					return DefaultLookAndFeel.Skin;
				}

				return mSkin;
			}
			set
			{
				//this.innerPanel.Size = new System.Drawing.Size(this.innerPanel.Size.Width - 17, this.innerPanel.Size.Height - 17);

				mSkin = value;
				Invalidate();
			}
		}

		#endregion

	}

	enum MoveType
	{ 
		None,
		Forward,
		Backward

	}
}
